﻿<UserControl 
  x:Class="TreeViewMVVMInPlaceEditingDemo.InPlaceEdit.InPlaceEditDemoControl"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:Behaviors="clr-namespace:TreeViewMVVMInPlaceEditingDemo.Behaviors">
  <DockPanel>
    <!-- FAMILY TREE -->
    <TreeView ItemsSource="{Binding FirstGeneration}" Behaviors:TreeViewInPlaceEditBehavior.IsEditable="True">
      <TreeView.ItemContainerStyle>
        <!-- 
        This Style binds a TreeViewItem to a PersonViewModel. 
        -->
        <Style TargetType="{x:Type TreeViewItem}">
          <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
          <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
        </Style>
      </TreeView.ItemContainerStyle>

      <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
          <Grid>
            <TextBlock Text="{Binding Name}" />
            <TextBox Text="{Binding Name, UpdateSourceTrigger=Explicit}" Visibility="Collapsed" x:Name="TextBox"/>
          </Grid>
          <HierarchicalDataTemplate.Triggers>
            <MultiDataTrigger>
              <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsSelected}" Value="True"/>
                <Condition Binding="{Binding Path=(Behaviors:TreeViewInPlaceEditBehavior.IsEditing), RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}}" Value="True"/>
              </MultiDataTrigger.Conditions>
              <Setter TargetName="TextBox" Property="Visibility" Value="Visible"/>
              <Setter TargetName="TextBox" Property="Behaviors:TreeViewInPlaceEditBoxBehavior.IsEditing">
                <Setter.Value>
                  <Binding Path="(Behaviors:TreeViewInPlaceEditBehavior.IsEditing)" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}"/>
                </Setter.Value>
              </Setter>
              <Setter TargetName="TextBox" Property="Behaviors:TreeViewInPlaceEditBoxBehavior.IsEditConfirmed">
                <Setter.Value>
                  <Binding Path="(Behaviors:TreeViewInPlaceEditBehavior.IsEditConfirmed)" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}"/>
                </Setter.Value>
              </Setter>
              <Setter TargetName="TextBox" Property="Behaviors:TreeViewInPlaceEditBoxBehavior.IsEditCancelled">
                <Setter.Value>
                  <Binding Path="(Behaviors:TreeViewInPlaceEditBehavior.IsEditCancelled)" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}"/>
                </Setter.Value>
              </Setter>
            </MultiDataTrigger>
          </HierarchicalDataTemplate.Triggers>
        </HierarchicalDataTemplate>
      </TreeView.ItemTemplate>
    </TreeView>

  </DockPanel>
</UserControl>
